package com.jt.service;

import com.jt.vo.ImageVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService{

    private String localDir = "D:/JT-SOFT/images";
    //private String localDir = "/usr/local/src/images";//linux测试
    private String perUrl = "http://image.jt.com";

    /**
     * 业务说明:
     *  1.校验图片的类型 是否为 jpg|png|gif
     *  2.校验文件是否为恶意程序  防止木马病毒.
     *  3.按照分目录结构 存储图片
     *  4.应该让文件名称不重复
     *
     * @param file
     * @return
     */
    @Override
    public ImageVO upload(MultipartFile file) {
        //1.校验图片的类型
        //1.1   获取图片名称   abc.gif ABC.GIF
        String fileName = file.getOriginalFilename();
        //1.2 将字母转化为小写
        fileName = fileName.toLowerCase();
        //1.3   正则表达式写法
        if(!fileName.matches("^.+\\.(jpg|png|gif)$")){
            return null;
        }

        //2.防止木马病毒  图片宽度和高度
        try {
            BufferedImage bufferedImage =
                    ImageIO.read(file.getInputStream());
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();

            if(width==0 || height==0){
                //说明 不是正经图片
                return null;
            }

            //3.分目录存储 提交的时间|名称hash abasedhh|type 进行存户
            String datePath = new SimpleDateFormat("/yyyy/MM/dd/")
                    .format(new Date());
            //3.1 创建图片目录
            String dirPath = localDir + datePath;
            //3.2 创建目录
            File dirFile = new File(dirPath);
            if(!dirFile.exists()){
                dirFile.mkdirs();
            }

            //4.要求文件名称不重名  uuid.type
            String uuid = UUID.randomUUID().toString();
            int index = fileName.lastIndexOf(".");
            //.jpg
            String type = fileName.substring(index);
            fileName = uuid + type;

            //5.实现文件上传
            String realFilePath = dirPath + fileName;
            file.transferTo(new File(realFilePath));
            //6.返回ImageVO对象
            String virtualPath = datePath + fileName;
            String urlPath = perUrl + virtualPath;
            ImageVO imageVO = new ImageVO(virtualPath, urlPath, fileName);
            System.out.println(urlPath);
            return imageVO;


        } catch (IOException e) {
            e.printStackTrace();
            //如果出现了异常信息,则返回null即可
            return null;
        }
        //22return null;
    }

    //删除文件
    @Override
    public void deleteFile(String virtualPath) {
        //1.指定文件删除路径
        String path = localDir + virtualPath;
        File file = new File(path);
        if(file.exists()){
            file.delete();
        }
    }


}

//    @Override
//    public ImageVO upload(MultipartFile file) {
//        //1.获取文件名称
//        String fileName = file.getOriginalFilename();
//        //2.指定磁盘路径信息
//        String filePath = "D:/JT-SOFT/image";
//        //3.检验目录是否存在
//        File dirFile = new File(filePath);
//        if(dirFile.exists()){
//            dirFile.mkdir();//创建多级目录
//        }
//        //4.准备文件的全路径
//        String path = filePath + "/" +fileName;
//        //5.将路径包装为file对象
//        File imageFile = new File(path);
//        //6.实现文件上传
//        try {
//            file.transferTo(imageFile);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
